<?php

class Admin_EcurieController extends Zend_Controller_Action
{

    public function init()
    {
		if(!Zend_Auth::getInstance()->hasIdentity())  
		{
			$this->_redirect('admin/login');  
		}  
    }

    public function indexAction()
    {
    }
    
    public function addAction()
    {
    	$form = new Application_Form_Ecurie();
    	$ecurieDAO = new Application_Model_EcurieDAO();
    	$paysDAO = new Application_Model_PaysDAO();
		$this->view->form = $form;
		if($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData))
			{
				$values = $form->getValues();
				$ecurie = new Application_Model_Ecurie();
				$ecurie->setPays($paysDAO->find($values['paynum']));
				$ecurie->setFpNum($values['fpnum']); //On ne se sert pas de cet element
				$ecurie->setEcuNom($values['ecunom']);
				$ecurie->setEcuNomDir($values['ecunomdir']);
				$ecurie->setEcuAdrSiege($values['ecuadrsiege']);
				$ecurie->setEcuAdresseImage($values['ecuadresseimage']);
				$ecurie->setEcuPoints(0); //On calcule dynamiquement les points

				if($ecurieDAO->save($ecurie))
				{
					$this->_redirect('admin/ecurie');
				}
				else
				{
					$form->populate($values);
				}
			}
			else
			{
				$form->populate($form->getValues());
			}
		}
    }
    
    public function updateAction()
    {
    	$ecurieDAO = new Application_Model_EcurieDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	        $form = new Application_Form_Ecurie();
	    	$paysDAO = new Application_Model_PaysDAO();
	    	$ecurie = $ecurieDAO->find($id);
	    	if(!is_null($ecurie))
	    	{
				$this->view->form = $form;
				$form->populate($ecurie->toArray());
				if($this->getRequest()->isPost())
				{
					$formData = $this->getRequest()->getPost();
					if($form->isValid($formData))
					{
						$values = $form->getValues();
						$ecurie = new Application_Model_Ecurie();
						$ecurie->setEcuNum($values['ecunum']);
						$ecurie->setPays($paysDAO->find($values['paynum']));
						$ecurie->setEcuNom($values['ecunom']);
						$ecurie->setEcuNomDir($values['ecunomdir']);
						$ecurie->setFpNum($values['fpnum']);
						$ecurie->setEcuPoints(0);
						$ecurie->setEcuAdrSiege($values['ecuadrsiege']);
						$ecurie->setEcuAdresseImage($values['ecuadresseimage']);
		
						if($ecurieDAO->save($ecurie))
						{
							$this->_redirect('admin/ecurie');
						}
						else
						{
							$form->populate($values);
						}
					}
					else
					{
						$form->populate($form->getValues());
					}
				}
			}
			else
			{
				$this->_redirect('admin/ecurie');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->ecuries = $ecurieDAO->findAll();
    	}
    }
	
    public function deleteAction()
    {
        $ecurieDAO = new Application_Model_EcurieDAO();
    	$id = $this->getRequest()->getParam('id');
    	echo $id;
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	    	$ecurie = $ecurieDAO->find($id);
	    	if(!is_null($ecurie))
	    	{
				$ecurieDAO->delete($ecurie->getEcuNum());
				$this->_redirect('admin/ecurie');
			}
			else
			{
				$this->_redirect('admin/ecurie');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->ecuries = $ecurieDAO->findAll();
    	}
    }
}